package cn.edu.cdtu.yulei.test_demo.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtil {

    private static final SqlSessionFactory sqlSessionFactory;

    // 加载配置文件
    static {
        try {
            InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 获取sqlSession实例
    public static SqlSession getSession(){
        // 通过SqlSessionFactory 获取 SqlSession
        return sqlSessionFactory.openSession();
    }

}